package com.junxonline.instant.api.controller;

import cn.hutool.core.util.StrUtil;
import com.junxonline.instant.api.service.api.ApiV1Service;
import com.junxonline.instant.common.annotation.ApiLog;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.ApiEnum;
import com.junxonline.instant.common.enumeration.ApiErrorEnum;
import com.junxonline.instant.common.enumeration.ApiVersionEnum;
import com.junxonline.instant.dao.entity.vo.device.TerminalVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 终端API V1相关Controller
 *
 * @author JunX
 * @date 2021-11-12
 */
@Log4j2
@RestController
@RequestMapping(value = "/api/v1")
public class ApiV1Controller {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ApiV1Service apiV1Service;

    /**
     * 获取服务器时间
     *
     * @return CommonResult
     */
    @ApiOperation(value = "获取服务器时间")
    @ApiLog(label = "获取服务器时间", type = ApiEnum.QUERY, version = ApiVersionEnum.V1)
    @PostMapping(value = "/getServerTime")
    public CommonResult getServerTime() {
        return CommonResult.success(apiV1Service.getServerTime());
    }

    /**
     * 上报终端信息
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @ApiOperation(value = "上报终端信息")
    @ApiLog(label = "上报终端信息", type = ApiEnum.EDIT, version = ApiVersionEnum.V1)
    @PostMapping(value = "/uploadInformation")
    public CommonResult uploadInformation(@RequestBody(required = false) TerminalVO vo) {
        if (vo.getType() == null
                || StrUtil.isBlank(vo.getHardwareCode())
                || StrUtil.isBlank(vo.getResolution())
                || StrUtil.isBlank(vo.getSoftwareVersion())
                || StrUtil.isBlank(vo.getOs())
                || StrUtil.isBlank(vo.getDiskSpace())
                || StrUtil.isBlank(vo.getIntranetIp())
                || StrUtil.isBlank(vo.getServerAddress())
        ) {
            return CommonResult.fail(ApiErrorEnum.PARAMS_IS_NULL);
        }
        String terminalCode = request.getHeader("terminalCode");
        vo.setCode(terminalCode);
        apiV1Service.uploadInformation(vo);
        return CommonResult.success();
    }

}
